﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebClient;
using DTO;
namespace WebClient.admin.Modules.cart
{
    public partial class Edit : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.btnAddNew.Attributes.Add("onclick", "return OnSubmitClick();");
            if (!this.IsPostBack)
            {
                InitItem();
            }
        }

        private void AddNew()
        {
            WebData data = new WebData();
            Cart item = new Cart();
            item.id =Convert.ToInt64(this.txtid.Text.Trim());
            item.customersId = Convert.ToInt64(this.txtcustomersId.Text.Trim());
            item.address = this.txtaddress.Text.Trim();
            item.create_on = Convert.ToDateTime(this.txtcreate_on.Text.Trim());
            item.total = Convert.ToDecimal(this.txttotal.Text.Trim());
            item.discount = Convert.ToDecimal(this.txtdiscount.Text.Trim());
            item.payment = Convert.ToDecimal(this.txtpayment.Text.Trim());
            item.arrears = 0;// Convert.ToDecimal(this.txtarrears.Text.Trim());
            item.credit = this.txtcredit.Text.Trim();
            item.status = Convert.ToInt32(cbbStatus.SelectedValue);
            item.orderId = Convert.ToInt32(this.txtorderId.Text.Trim());
            item.isActive = Convert.ToInt32(this.txtisActive.Text.Trim());
            item.fullName = this.txtfullName.Text.Trim();
            item.phone = this.txtphone.Text.Trim();
            if (data.OnSubmitCart(ref item, 1)) {
                Response.Redirect(ViewState["URLReferrer"].ToString());
            }
        }
        private void InitItem()
        {
            ViewState["URLReferrer"] = Request.UrlReferrer.AbsoluteUri;
            WebData db = new WebData();
            Cart item = new Cart();
            long cartId = -1;
            try {
                cartId = Convert.ToInt64(Request.Params["ID1"]);
            }
            catch { }
            item = db.GetItemByIdCart(cartId);
            if (item != null)
            {
                txtid.Text = item.id.ToString();
                txtcustomersId.Text = item.customersId.ToString();
                txtaddress.Text = item.address.ToString();
                txtcreate_on.Text = item.create_on.ToString();
                txttotal.Text = item.total.ToString();
                txtdiscount.Text = item.discount.ToString();
                txtpayment.Text = item.payment.ToString();
                txtarrears.Text = item.arrears.ToString();
                txtcredit.Text = item.credit.ToString();
                cbbStatus.SelectedValue = item.status.ToString();
                txtorderId.Text = item.orderId.ToString();
                txtisActive.Text = item.isActive.ToString();
                txtfullName.Text = item.fullName.ToString();
                txtphone.Text = item.phone.ToString();
            }
            LoadCartDetail(cartId);
        }

        private void LoadCartDetail(long cartId) {
            WebData db = new WebData();
            this.grdDs.DataSource = db.GetAllByCartDetailByCartId(cartId);
            this.grdDs.DataBind();
        }
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            AddNew();
        }
    }
}